package com.example.demoshiro.common.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @GetMapping("/test1")
    public String index(HttpServletRequest request){
        HttpSession session = request.getSession();
        return "test1";
    }

    @GetMapping("/test2")
    public String index2(){
        return "test2";
    }
    @GetMapping("/test3")
    public String index3(){
        return "test3";
    }
    @PostMapping("/do/login")
    public String doLogin(HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(request.getParameter("username"), request.getParameter("password"));
        try {
            subject.login(token);
        }catch (AuthenticationException ae){
            return "404";
        }
        return "200";
    }
}
